﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ContagemFieiras
{
    public partial class frmEntrada : Form
    {
        #region Declarações
        public static DataView dvUsers;
        public static SqlDataAdapter daLinhasTarefas, daProducao;
        readonly frmLogin _login = new frmLogin();
        #endregion

        public frmEntrada()
        {
            InitializeComponent();
            Application.Idle += new EventHandler(Application_Idle);
        }

        private void Application_Idle(object sender, EventArgs e)
        { tmrLogin.Enabled = true; }

        public void tmr_Stop()
        { tmrLogin.Enabled = false; }

        private void frmEntrada_Load(object sender, EventArgs e)
        {
            MinimumSize = Screen.PrimaryScreen.WorkingArea.Size;
            MaximumSize = MinimumSize;
            Location = new Point(0, 0);

            Conn.ConnectionString = Properties.Settings.Default.connStr;

            try
            {
                Conn.Open();
                Conn.Close();
            }
            catch
            {
                frmSql sql = new frmSql();
                sql.ShowDialog();
                FormClosing -= frmEntrada_FormClosing;
                Close();
                Application.Exit();
            }

            daUsers.Fill(dsFilkemp.Tables["dtUsers"]);
            dvUsers = new DataView(dsFilkemp.Tables["dtUsers"]);
            daLinhasTarefas = new SqlDataAdapter("Select * from Linhas_Tarefas", Conn);
            daProducao = new SqlDataAdapter("Select * from Producao", Conn);

            frmLogin login = new frmLogin();
            login.ShowDialog();
        }

        private void btnSaida_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmEntrada_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Deseja sair?", "", MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
        }

        public void daLinhas_TarefasFill()
        { daLinhasTarefas.Fill(dsFilkemp, "dtLinhas_Tarefas"); }

        public void daProducaoFill()
        {
            daProducao.Fill(dsFilkemp, "dtProducao");
        }

        public void statusLabel(string s)
        {
            lblStatus.Text = s;
        }

        private void btnExportar_Click(object sender, EventArgs e)
        {
            dtProducao.Clear();
            daProducao.Fill(dsFilkemp, "dtProducao");

            DataView dvProducao = new DataView(dsFilkemp.Tables["dtProducao"]);

            if (dvProducao.Count > 0)
            {
                SaveFileDialog svFile = new SaveFileDialog();
                svFile.AddExtension = true;
                svFile.Filter = "Vírgula Flutuante(*.csv)|*.csv|Ficheiro de Texto(*.txt)|*.txt";
                svFile.DefaultExt = "*.*";

                if (svFile.ShowDialog() == DialogResult.OK) dataTableToTextFile(dvProducao, svFile.FileName);
            }
            else
                MessageBox.Show("Não tem dados para gravar.");
        }

        private void dataTableToTextFile(DataView dvToText, string filePath)
        {
            StreamWriter sw;
            try
            {
                sw = new StreamWriter(filePath);
                for (int i = 1; i < dvToText.Table.Columns.Count; i++) sw.Write(dvToText.Table.Columns[i].ColumnName + ";");
                sw.WriteLine();

                //For Data in the Rows
                foreach (DataRowView row in dvToText)
                {
                    for (int i = 1; i < dvToText.Table.Columns.Count; i++) sw.Write(row[i] + ";");
                    sw.WriteLine();
                }
                sw.Close();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void tmrLogin_Tick(object sender, EventArgs e)
        {
            bool isOpened = false;
            foreach (Form f in Application.OpenForms) if (f.Name == "frmLogin") isOpened = true;
            if (!isOpened) _login.ShowDialog();            
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            btnLock.Image = Properties.Resources.LOCK;
            tmr_Stop();

            frmLogin login = new frmLogin();
            login.ShowDialog(this);
        }
    }
}